/*
 * 用户反馈 广告列表 
 */
$(function(){
	index();
	var startTime="";
	var endTime="";
	var adName="";
	var sT='';
	var eT='';
	var aN='';
	$("#searchbtn").on("click",query);//点击查询触发

	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	};




	var page
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if (page == activeLi){
			}else{
				if(page==1){
					index2(page);
				}else if(page>showPageN-4){
				}else{
					total(page);
				}
			}

		}
		$("input[name='selectPage']").val("");
	});

	//输入页码 点击确定 事件
	function total(page){
		if(page.length !=0){
			startTime=$("#startDay").val();
			endTime=$("#endDay").val();
			adName=$("#adName").val();
			
			$.ajax({
				url : '/web-adv/feedbackAd',
		        data:{"startTime":startTime,"endTime":endTime,"adName":adName,"page":page},
				type : 'post',
				dataType:"json",
				success : function(data) {
					sT=data.startTime;
					eT=data.endTime;
					var obj=data.beans;
					//console.log(obj)
					var totalComments=0;
					var complainNum=0;
					var str='<tr>\
						     	<th>序号</th>\
						     	<th>广告名称</th>\
						     	<th>广告编码</th>\
						     	<th>广告形式</th>\
						     	<th>投放终端</th>\
						     	<th>评论数</th>\
								<th>投诉</th>\
								<th>操作</th>\
							</tr>';
					for(var i=0;i<obj.length;i++){
						//计算百分比
						var pers = getPercent(obj[i].praiseNum,obj[i].stepnum);
						var praiseNumPer = "";
						var stepNumPer = "";
						if(pers.length !=0){
							praiseNumPer = pers[0];
							stepNumPer = pers[1];
						}
						var pare = "selectedNav=5&navPos=0&st="+sT+"&eT="+eT+"&adId="+obj[i].adId+"&praiseNumPer="+praiseNumPer+"&stepNumPer="+stepNumPer+"'";
						var hreflink="javascript:location.href='/web-adv/advWeb/views/feedback/feedbackOneday.html?"+pare;
						alert(obj[i].adType);
						if(obj[i].adType =="video"){
							var viewadUrl = "/web-adv/advWeb/views/adManagement/vedioAd/queryVedioAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;
						}else{
							var viewadUrl = "/web-adv/advWeb/views/adManagement/pictureAd/queryPictureAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;

						}
						str+='<tr>\
							<td>'+(i+1)+'</td>\
						 	<td>'+obj[i].adName+'</td>\
						 	<td><a href='+viewadUrl+'>'+obj[i].adCode+'</a></td>\
						 	<td>'+transitionAdType(obj[i].adType)+'</td>\
						 	<td>'+transitionTerminalTyp(obj[i].terminalType)+'</td>\
						 	<td class="commentNum">'+obj[i].commentNum+'</td>\
						 	<td>'+(!obj[i].complain  ? obj[i].complain:0)+'</td>\
						 	<td><a href="'+hreflink+'" class="btn btn-primary">单日查看</a></td>\
						 </tr>';
						totalComments+=parseInt(obj[i].commentNum);
						complainNum +=parseInt((!obj[i].complain  ? obj[i].complain:0));
						
					}
					str+='<tr class="table-hover-total">'
					 	+'<td>总计</td>'
					 	+'<td></td>'
					 	+'<td></td>'
					 	+'<td></td>'
					 	+'<td></td>'
					 	+'<td class="commentNums">'+totalComments+'</td>'
					 	+'<td>'+complainNum+'</td>'
					 	+'<td></td>'		
					 	+'</tr>';
					$(".table-hover").html(str);
					/*if($(".commentNums").html()==0){
						alert("亲，换个关键字试一试吧")
					}*/
				
					
					startTime=$("#startDay").val("");
					endTime=$("#endDay").val("");
					
					$("#startDay").val(sT);
					$("#endDay").val(eT);
				},
		        error: function(){}        
			});
		}
		
	}	
	
	
/**
 * 转换  广告类型
 */
function transitionAdType(adType){
	switch (adType){
	   case "video":
		  adType ="视频";
	     break;
	   case "picture":
		  adType ="图片";
	     break;
	   default:
	     adType ="";
	}
	
	return adType;
}

/**
 * 转换  mobile，pc，pad，tv
 */
function transitionTerminalTyp(terminalType){
	switch (terminalType) {
	   case "mobile":
		   terminalType ="手机";
	     break;
	   case "pc":
		   terminalType =" PC";
	     break;
	   case "pad":
		   terminalType ="PAD";
	     break;
	   case "tv":
		   terminalType ="互联网电视";
	     break;
	   default:
		   terminalType ="";
	}
	
	return terminalType;
}

function GetRequest() {
    var url = decodeURI(location.search);
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

function index(){
	var object = GetRequest();
	startTime = object.startTime ? object.startTime:$("#startDay").val();
	endTime = object.endTime ?object.endTime:$("#endTime").val();
	adName = object.adName ?object.adName:$("#adName").val();
	$("#adName").val(adName);
	console.log("startTime="+startTime+";endTime="+endTime+";adName="+adName);
	/*startTime=$("#startDay").val();
	endTime=$("#endDay").val();
	adName=$("#adName").val();*/
	$.ajax({
		url : '/web-adv/feedbackAd',
        data:{"startTime":startTime,"endTime":endTime,"adName":adName,"page":"1"},
		type : 'post',
		dataType:"json",
		success : function(data) {
			sT=startTime?startTime:data.startTime;
			eT=endTime?endTime:data.endTime;
			var obj=data.beans;

			var totalComments=0;
			var complainNum=0;
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	index2(page);	
	            }
			});
			
			var str='<tr>\
				     	<th>序号</th>\
				     	<th>广告名称</th>\
				     	<th>广告编码</th>\
				     	<th>广告形式</th>\
				     	<th>投放终端</th>\
				     	<th>评论数</th>\
						<th>投诉</th>\
						<th>操作</th>\
					</tr>';
			for(var i=0;i<obj.length;i++){
				//计算百分比
				var pers = getPercent(obj[i].praiseNum,obj[i].stepnum);
				var praiseNumPer = "";
				var stepNumPer = "";
				if(pers.length !=0){
					praiseNumPer = pers[0];
					stepNumPer = pers[1];
				}
				var pare = "selectedNav=5&navPos=0&st="+sT+"&eT="+eT+"&adId="+obj[i].adId+"&praiseNumPer="+praiseNumPer+"&stepNumPer="+stepNumPer+"'";
				var hreflink="javascript:location.href='/web-adv/advWeb/views/feedback/feedbackOneday.html?"+pare;

				if(obj[i].adType =="video"){
					var viewadUrl = "/web-adv/advWeb/views/adManagement/vedioAd/queryVedioAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;
				}else{
					var viewadUrl = "/web-adv/advWeb/views/adManagement/pictureAd/queryPictureAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;

				}

				str+='<tr>\
					<td>'+(i+1)+'</td>\
				 	<td>'+obj[i].adName+'</td>\
				 	<td><a href='+viewadUrl+'>'+obj[i].adCode+'</a></td>\
				 	<td>'+transitionAdType(obj[i].adType)+'</td>\
				 	<td>'+transitionTerminalTyp(obj[i].terminalType)+'</td>\
				 	<td class="commentNum">'+obj[i].commentNum+'</td>\
				 	<td>'+(obj[i].complain  ? obj[i].complain:0)+'</td>\
				 	<td><a href="'+hreflink+'" class="btn btn-primary">单日查看</a></td>\
				 </tr>';
				totalComments+=parseInt(obj[i].commentNum);
				complainNum+=parseInt((obj[i].complain  ? obj[i].complain:0));
				
			}
			str+='<tr class="table-hover-total">'
			 	+'<td>总计</td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td class="commentNums">'+totalComments+'</td>'
			 	+'<td>'+complainNum+'</td>'
			 	+'<td></td>'		
			 	+'</tr>';
			$(".table-hover").html(str);
			/*if($(".commentNums").html()==0){
				alert("亲，换个关键字试一试吧")
			}*/
		
			
			startTime=$("#startDay").val("");
			endTime=$("#endDay").val("");
			
			$("#startDay").val(sT);
			$("#endDay").val(eT);
		},
        error: function(){}        
	});
}
function index2(page){
	
	startTime=$("#startDay").val();
	endTime=$("#endDay").val();
	adName=$("#adName").val();
	
	$.ajax({
		url : '/web-adv/feedbackAd',
        data:{"startTime":startTime,"endTime":endTime,"adName":adName,"page":page},
		type : 'post',
		dataType:"json",
		success : function(data) {
			sT=data.startTime;
			eT=data.endTime;
			var obj=data.beans;
			//console.log(obj)
			var totalComments=0;
			var complainNum=0;
			var str='<tr>\
				     	<th>序号</th>\
				     	<th>广告名称</th>\
				     	<th>广告编码</th>\
				     	<th>广告形式</th>\
				     	<th>投放终端</th>\
				     	<th>评论数</th>\
						<th>投诉</th>\
						<th>操作</th>\
					</tr>';
			for(var i=0;i<obj.length;i++){
				//计算百分比
				var pers = getPercent(obj[i].praiseNum,obj[i].stepnum);
				var praiseNumPer = "";
				var stepNumPer = "";
				if(pers.length !=0){
					praiseNumPer = pers[0];
					stepNumPer = pers[1];
				}
				var pare = "selectedNav=5&navPos=0&st="+sT+"&eT="+eT+"&adId="+obj[i].adId+"&praiseNumPer="+praiseNumPer+"&stepNumPer="+stepNumPer+"'";
				var hreflink="javascript:location.href='/web-adv/advWeb/views/feedback/feedbackOneday.html?"+pare;
				if(obj[i].adType =="video"){
					var viewadUrl = "/web-adv/advWeb/views/adManagement/vedioAd/queryVedioAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;
				}else{
					var viewadUrl = "/web-adv/advWeb/views/adManagement/pictureAd/queryPictureAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;

				}
				str+='<tr>\
					<td>'+(i+1)+'</td>\
				 	<td>'+obj[i].adName+'</td>\
					<td><a href='+viewadUrl+'>'+obj[i].adCode+'</a></td>\
				 	<td>'+transitionAdType(obj[i].adType)+'</td>\
				 	<td>'+transitionTerminalTyp(obj[i].terminalType)+'</td>\
				 	<td class="commentNum">'+obj[i].commentNum+'</td>\
				 	<td>'+(obj[i].complain  ? obj[i].complain:0)+'</td>\
				 	<td><a href="'+hreflink+'" class="btn btn-primary">单日查看</a></td>\
				 </tr>';

				totalComments+=parseInt(obj[i].commentNum);
				complainNum+=parseInt((obj[i].complain  ? obj[i].complain:0));

			}
			str+='<tr class="table-hover-total">'
			 	+'<td>总计</td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td class="commentNums">'+totalComments+'</td>'
			 	+'<td>'+complainNum+'</td>'
			 	+'<td></td>'		
			 	+'</tr>';
			$(".table-hover").html(str);
			/*if($(".commentNums").html()==0){
				alert("亲，换个关键字试一试吧")
			}*/
		
			
			startTime=$("#startDay").val("");
			endTime=$("#endDay").val("");
			
			$("#startDay").val(sT);
			$("#endDay").val(eT);
			if(adName.length !=0){
				
				$("#adName").val(adName);
			}
		},
        error: function(){}        
	});
}	

function query(){
	
	startTime=$("#startDay").val();
	endTime=$("#endDay").val();
	adName=$("#adName").val();
	
	$.ajax({
		url : '/web-adv/feedbackAd',
        data:{"startTime":startTime,"endTime":endTime,"adName":adName,"page":page},
		type : 'post',
		dataType:"json",
		success : function(data) {
			sT=data.startTime;
			eT=data.endTime;
			var obj=data.beans;
			//console.log(obj)
			var totalComments=0;
			var compalinNum=0;
			var str='<tr>\
				     	<th>序号</th>\
				     	<th>广告名称</th>\
				     	<th>广告编码</th>\
				     	<th>广告形式</th>\
				     	<th>投放终端</th>\
				     	<th>评论数</th>\
						<th>投诉</th>\
						<th>操作</th>\
					</tr>';
			for(var i=0;i<obj.length;i++){
				//计算百分比
				var pers = getPercent(obj[i].praiseNum,obj[i].stepnum);
				var praiseNumPer = "";
				var stepNumPer = "";
				if(pers.length !=0){
					praiseNumPer = pers[0];
					stepNumPer = pers[1];
				}
				var pare = "selectedNav=5&navPos=0&st="+sT+"&eT="+eT+"&adId="+obj[i].adId+"&praiseNumPer="+praiseNumPer+"&stepNumPer="+stepNumPer+"'";
				var hreflink="javascript:location.href='/web-adv/advWeb/views/feedback/feedbackOneday.html?"+pare;
				if(obj[i].adType =="video"){
					var viewadUrl = "/web-adv/advWeb/views/adManagement/vedioAd/queryVedioAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;
				}else{
					var viewadUrl = "/web-adv/advWeb/views/adManagement/pictureAd/queryPictureAd.html?selectedNav=2&navPos=0&adId="+obj[i].adId;

				}
				str+='<tr>\
					<td>'+(i+1)+'</td>\
				 	<td>'+obj[i].adName+'</td>\
				 	<td><a href='+viewadUrl+'>'+obj[i].adCode+'</a></td>\
				 	<td>'+transitionAdType(obj[i].adType)+'</td>\
				 	<td>'+transitionTerminalTyp(obj[i].terminalType)+'</td>\
				 	<td class="commentNum">'+obj[i].commentNum+'</td>\
				 	<td>'+(obj[i].complain  ? obj[i].complain:0)+'</td>\
				 	<td><a href="'+hreflink+'" class="btn btn-primary">单日查看</a></td>\
				 </tr>';
				totalComments+=parseInt(obj[i].commentNum);
				compalinNum+=parseInt((obj[i].complain  ? obj[i].complain:0));
				
			}
			str+='<tr class="table-hover-total">'
			 	+'<td>总计</td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td></td>'
			 	+'<td class="commentNums">'+totalComments+'</td>'
			 	+'<td>'+compalinNum+'</td>'
			 	+'<td></td>'		
			 	+'</tr>';
			$(".table-hover").html(str);
			/*if($(".commentNums").html()==0){
				alert("亲，换个关键字试一试吧")
			}*/
		
			
			startTime=$("#startDay").val("");
			endTime=$("#endDay").val("");
			
			$("#startDay").val(sT);
			$("#endDay").val(eT);
		},
        error: function(){}        
	});
}	

	function getPercent(praiseNum,stepnum){
		var pers = new Array();
		if(praiseNum ==null && stepnum==null ){
			pers.push("0");
			pers.push("0");
			return pers
		}
		praiseNum = parseFloat(praiseNum);
		stepnum = parseFloat(stepnum);
		var total = parseFloat(praiseNum+stepnum);
		if(praiseNum != 0 && stepnum != 0){
			var praiseNumPer = Math.round(praiseNum / total * 10000) / 100.00;
			var stepNumPer = 100-praiseNumPer;
			pers.push(praiseNumPer+"%");
			pers.push(stepNumPer+"%");

		}else if(praiseNum ==0 && stepnum != 0){
			pers.push("0");
			pers.push("100.00%");
		}else if(praiseNum ==0 && stepnum != 0){
			pers.push("100.00%");
			pers.push("0");
		}else{

		}
		return pers;

	}
});